package com.haiyou.dc.api.controller;

import com.haiyou.common.exception.BusinessException;
import com.haiyou.common.util.R;
import com.haiyou.common.util.RedisUtil;
import com.haiyou.oss.alibaba.service.AliOSSService;
import com.haiyou.sms.alibaba.service.AliSmsService;
import com.haiyou.sms.tencent.service.TencentSmsService;
import com.haiyu.oss.tencent.service.TencentOSSService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @author root
 * @date 2020-01-04 18:37:46
 */
@RestController
@RequestMapping("/sms")
@Slf4j
@Api(tags = "测试sms-start")
@Validated
public class SmsController {

    @Autowired
    private AliSmsService aliYunSmsService;
    @Autowired
    private TencentSmsService tencentYunSmsService;
    @Autowired
    private AliOSSService aliYunOSSService;
    @Autowired
    private TencentOSSService tencentOSSService;
    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value = "发送短信")
    @GetMapping("/send")
    public R info() {
//        JSONObject object = new JSONObject();
//        String randCode = aliYunSmsService.getRandCode(6);
//        log.info("验证码为：{}", randCode);
//        object.put("code", randCode);
//        aliYunSmsService.sendSms("15013349143", "郑鼎记创新点心", "SMS_186616976", object.toString());
        aliYunSmsService.sendLoginSms("15013349143", () -> {
            System.out.println("发送前检查");
            throw new BusinessException("生成失败");
        });
        System.out.println("发送成功");
//        aliYunSmsService.sendForgetPswdSms("15013349143", object.toString());
//        String code = tencentYunSmsService.getRandCode(6);
//        tencentYunSmsService.sendLoginSms("15013349143", new String[]{code, "30"});
        aliYunSmsService.sendRegisterSms("15013349143",()->{
            System.out.println("发送前检查");
            throw new BusinessException("生成失败");
        });

        aliYunSmsService.sendForgetPswdSms("15013349143",()->{
            System.out.println("发送前检查");
            throw new BusinessException("生成失败");
        });

        return R.ok();
    }

    @ApiOperation(value = "检查短信")
    @GetMapping("/checkCode")
    public R checkCode(@RequestParam(value = "code") String code) {
//        String sendCode = aliYunSmsService.getRandCode(6);
//        redisUtil.set(RedisKeys.getSmsLoginKey("15013349143"), sendCode);
        aliYunSmsService.checkRegisterSms("15013349143", code);
        return R.ok();
    }

    @ApiOperation(value = "oss测试")
    @GetMapping("/oss")
    public R oss() throws FileNotFoundException {
        File file = new File("F:\\b.jpg");
        InputStream input = new FileInputStream(file);
        String s = aliYunOSSService.upLoad(input);
        return R.ok(s);
    }

    @ApiOperation(value = "tt_oss测试")
    @GetMapping("/tt_oss")
    public R tt_oss() {
        String filePath = "F:\\b.jpg";
        String s = tencentOSSService.upload2Cos(filePath);
        return R.ok(s);
    }

    @ApiOperation(value = "aa_oss测试")
    @GetMapping("/aa_oss")
    public R aa_oss() {
        String url = "http://localhost/a.jpg";
        RestTemplate rest = new RestTemplate();
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        HttpEntity requestEntity = new HttpEntity(headers);
        ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
        String s = tencentOSSService.upload2Cos(entity.getBody());
        return R.ok(s);
    }


}